package gitternetz;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Point;

import javax.swing.JComponent;
import javax.swing.JFrame;

public class Gitternetz extends JFrame {
	int netzweite;
	
	public static void main(String[] args) {
		new Gitternetz(1,Color.red.getRGB(),Color.blue.getRGB(),Color.yellow.getRGB(),Color.green.getRGB());
	}
	
	Gitternetz(final int netzweite,final int c1,final int c2,final int c3,final int c4) {
		setLayout(new GridLayout());
		setSize(500,500);
		add(new JComponent() {
			public void paintComponent(Graphics g) {
				drawGitternetz(g, netzweite, c1,c2,c3,c4);
			}
		});
		setVisible(true);
	}

	int singleShuffle(int i1_part, int i2_part, int p) {
		return i1_part + (i2_part - i1_part) * p / 100;
	}

	int colorShuffle(int i1, int i2, int p) {
		int red = singleShuffle((i1 >> 16) & 255, (i2 >> 16) & 255, p);
		int green = singleShuffle((i1 >> 8) & 255, (i2 >> 8) & 255, p);
		int blue = singleShuffle((i1) & 255, (i2) & 255, p);
		return (255 << 24) | (red << 16) | (green << 8) | blue;
	}
	
//	public void drawLineBasic(Graphics g, int x1, int y1, int x2, int y2,
//			int cStart, int cEnd){
//		float dx=x2-x1;
//		float dy=y2-y1;
//		float D = dy/dx,y=y1;
//		int x = x1;
//		double fullLength = Point.distance(x1, y1, x2, y2);
//		for(int i=0;i<=dx;++i){
//			g.drawLine(x, (int) y, x, (int) y);
//			double partLength = Point.distance(x, y, x2, y2);
//			int p = (int) ((partLength / fullLength) * 100);
//			g.setColor(new Color(colorShuffle(cStart, cEnd, p)));
//			++x;
//			y=y+D;
//			
//		}
//	}
	
	public void drawLine(Graphics g, int x1, int y1, int x2, int y2,
			int cStart, int cEnd) {
		final int dx = Math.abs(x1 - x2);
		final int dy = Math.abs(y1 - y2);
		final int sgnDx = x1 < x2 ? 1 : -1;
		final int sgnDy = y1 < y2 ? 1 : -1;
		int shortD, longD, incXshort, incXlong, incYshort, incYlong;

		if (dx > dy) {
			shortD = dy;
			longD = dx;
			incXlong = sgnDx;
			incXshort = 0;
			incYlong = 0;
			incYshort = sgnDy;
		} else {
			shortD = dx;
			longD = dy;
			incXlong = 0;
			incXshort = sgnDx;
			incYlong = sgnDy;
			incYshort = 0;
		}

		int d = longD / 2, x = x1, y = y1;

		double fullLength = Point.distance(x1, y1, x2, y2);
		
		for (int i = 0; i <= longD; ++i) {
			double partLength = Point.distance(x, y, x2, y2);
			int p = (int) ((partLength / fullLength) * 100);
			g.setColor(new Color(colorShuffle(cStart, cEnd, p)));
			g.drawLine(x, y, x, y);

			
						
			x += incXlong;
			y += incYlong;
			d += shortD;
			if (d >= longD) {
				d -= longD;
				x += incXshort;
				y += incYshort;
			}
		}
	}

	
	
	public int getColorAtCoordinates(int x, int y,int c1, int c2, int c3, int c4){
		//int p = x < y ? ((x+1)/(y+1))*100 : ((y+1)/(x+1))*100;System.out.println(p); 
		int p = y*100/getHeight();
		return colorShuffle(colorShuffle(c1,c2, x*100/getWidth()),colorShuffle(c3,c4,x*100/getWidth()),p  );
	}

	public void drawGitternetz(Graphics g, int netzweite, int c1, int c2,
			int c3, int c4) {
		// zeichne vertikale Geraden
		for (int x = 0; x < getWidth(); x += netzweite) {
			drawLine(g,x,0,x,getHeight(),getColorAtCoordinates(x,0,c1,c2,c3,c4),getColorAtCoordinates(x,getHeight(),c1,c2,c3,c4));
		}

		// zeichne horizontale Geraden
		for (int y = 0; y < getHeight(); y += netzweite) {
			drawLine(g,0,y,getWidth(),y,getColorAtCoordinates(0,y,c1,c2,c3,c4),getColorAtCoordinates(getWidth(),y,c1,c2,c3,c4));
		}
//		drawLineBasic(g,0,0,getWidth(),getHeight(),getColorAtCoordinates(0,0,c1,c2,c3,c4),getColorAtCoordinates(getWidth(),getHeight(),c1,c2,c3,c4));

	}
}
